package com.gh.manager.train.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.gh.common.core.domain.model.LoginUser;
import com.gh.manager.train.constant.TrainConstant;
import com.gh.manager.train.dto.TrainDetailDto;
import com.github.pagehelper.PageHelper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.gh.common.annotation.Log;
import com.gh.common.core.controller.BaseController;
import com.gh.common.core.domain.AjaxResult;
import com.gh.common.enums.BusinessType;
import com.gh.manager.train.domain.GhTrain;
import com.gh.manager.train.service.IGhTrainService;
import com.gh.common.utils.poi.ExcelUtil;
import com.gh.common.core.page.TableDataInfo;

/**
 * 训练Controller
 * 
 * @author andas
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/manager/train")
public class GhTrainController extends BaseController
{
    @Autowired
    private IGhTrainService ghTrainService;

    /**
     * 报名参加训练
     * @param ghTrain
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manager:train:list')")
    @PostMapping("/join")
    public AjaxResult join(@RequestBody GhTrain ghTrain)
    {
        Long trainId = ghTrain.getTrainId();
        Long userId = getUserId();
        ghTrainService.join(trainId, userId, TrainConstant.TRAIN_TYPE_DX);
        return null;
    }

    /**
     * 查询训练包含哪些用户
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manager:train:list')")
    @PostMapping("/trainToUser")
    public AjaxResult trainToUser(GhTrain ghTrain)
    {
        TrainDetailDto resultDto = ghTrainService.selectUserByTrainId(ghTrain, TrainConstant.TRAIN_TYPE_DX);
        return success(resultDto);
    }

    /**
     * 查询用户参加了哪些训练
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manager:train:list')")
    @PostMapping("/userToTrain")
    public TableDataInfo userToTrain()
    {

        return null;
    }

    /**
     * 查询训练列表
     */
    @PreAuthorize("@ss.hasPermi('manager:train:list')")
    @GetMapping("/list")
    public TableDataInfo list(GhTrain ghTrain)
    {

        Date now = new Date();
        String grade = ghTrain.getGrade();
        if (grade == null || "".equals(grade)) {
            LoginUser loginUser = getLoginUser();
            if (loginUser.getUser().getGrade() != null) {
                grade = loginUser.getUser().getGrade();
            }
            ghTrain.setGrade(grade);
        }

        startPage();  // 这是若依框架的分页方法
        // 设置排序字符串
        String orderBy = "status asc,create_time desc";
        PageHelper.getLocalPage().setUnsafeOrderBy(orderBy);
        List<GhTrain> list = ghTrainService.selectGhTrainList(ghTrain);
        list.forEach(item->{
            Date overtime = item.getTime();
            if (overtime != null && overtime.getTime() > now.getTime()) {
                item.setStatus(1L);
            }else if (overtime != null && overtime.getTime() < now.getTime()) {
                item.setStatus(0L);
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出训练列表
     */
    @PreAuthorize("@ss.hasPermi('manager:train:export')")
    @Log(title = "训练", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GhTrain ghTrain)
    {
        List<GhTrain> list = ghTrainService.selectGhTrainList(ghTrain);
        ExcelUtil<GhTrain> util = new ExcelUtil<GhTrain>(GhTrain.class);
        util.exportExcel(response, list, "训练数据");
    }

    /**
     * 获取训练详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:train:query')")
    @GetMapping(value = "/{trainId}")
    public AjaxResult getInfo(@PathVariable("trainId") Long trainId)
    {
        return success(ghTrainService.selectGhTrainByTrainId(trainId));
    }

    /**
     * 新增训练
     */
    @PreAuthorize("@ss.hasPermi('manager:train:add')")
    @Log(title = "训练", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GhTrain ghTrain)
    {
        return toAjax(ghTrainService.insertGhTrain(ghTrain));
    }

    /**
     * 修改训练
     */
    @PreAuthorize("@ss.hasPermi('manager:train:edit')")
    @Log(title = "训练", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GhTrain ghTrain)
    {
        return toAjax(ghTrainService.updateGhTrain(ghTrain));
    }

    /**
     * 删除训练
     */
    @PreAuthorize("@ss.hasPermi('manager:train:remove')")
    @Log(title = "训练", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainIds}")
    public AjaxResult remove(@PathVariable Long[] trainIds)
    {
        return toAjax(ghTrainService.deleteGhTrainByTrainIds(trainIds));
    }
}
